﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace AMing.Mvc4.Web
{
    public class HostnameHelper
    {

        /// <summary>
        /// 初始化HostnameHelper
        /// </summary>
        /// <param name="request"></param>
        public HostnameHelper(HttpRequestBase request)
        {
            this.Request = request;
        }

        /// <summary>
        /// 为当前 HTTP 请求获取 System.Web.HttpRequestBase 对象。
        /// </summary>
        public HttpRequestBase Request { get; set; }

        /// <summary>
        /// 主机名跳转
        /// </summary>
        /// <param name="defaultActionResult"></param>
        /// <param name="hostnames"></param>
        /// <returns></returns>
        public virtual ActionResult HostnameJump(ActionResult defaultActionResult, params HostNameActionResult[] hostnames)
        {
            string hostname = this.Request.Url.Authority.ToLower();
            foreach (var item in hostnames)
            {
                string host = HostNameData.GetHostName(item.Key);
                if (host != null)
                {
                    if (hostname.Contains(host))
                    {
                        return item.Value;
                    }
                }
            }

            return defaultActionResult;
        }

    }


}